/*******************************************************************************
 * Copyright 2011 See AUTHORS file.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

package com.asg.guesswhat.screens;

import java.sql.SQLException;

import com.asg.guesswhat.Assets;
import com.asg.guesswhat.OverlapTester;
import com.asg.guesswhat.Settings;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.GLCommon;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector3;

@SuppressWarnings("unused")
public class MainMenuScreen extends GWScreen {
	Game game;

	OrthographicCamera guiCam;
	SpriteBatch batcher;
	Rectangle[] gameBounds;
	Rectangle newGameBounds;
	//Rectangle highscoresBounds;
	//Rectangle helpBounds;
	Vector3 touchPoint;

	public MainMenuScreen (Game game) {
		//this.game = game;
		super(game);
		guiCam = new OrthographicCamera(320, 480);
		guiCam.position.set(320 / 2, 480 / 2, 0);
		batcher = new SpriteBatch();
		//soundBounds = new Rectangle(0, 0, 64, 64);
		//playBounds = new Rectangle(160 - 150, 200 + 18, 300, 36);
		newGameBounds = new Rectangle(160 - 240 / 2, 480 - 92 - 50, 300, 36);
		//highscoresBounds = new Rectangle(160 - 150, 200 - 18, 300, 36);
		//helpBounds = new Rectangle(160 - 150, 200 - 18 - 36, 300, 36);
		touchPoint = new Vector3();
	}

	public void update (float deltaTime) {
		if (Gdx.input.justTouched()) {
			guiCam.unproject(touchPoint.set(Gdx.input.getX(), Gdx.input.getY(), 0));
			if (OverlapTester.pointInRectangle(newGameBounds, touchPoint.x, touchPoint.y)) {
				Assets.playSound(Assets.clickSound);
				/*try {
					//Assets.sqlHandler.hostNewGame(Settings.hostID);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				//game.setScreen(new GameScreen(game));*/
				return;
			}
			/*if (OverlapTester.pointInRectangle(highscoresBounds, touchPoint.x, touchPoint.y)) {
				Assets.playSound(Assets.clickSound);
				game.setScreen(new HighscoresScreen(game));
				return;
			}
			if (OverlapTester.pointInRectangle(helpBounds, touchPoint.x, touchPoint.y)) {
				Assets.playSound(Assets.clickSound);
				game.setScreen(new HelpScreen(game));
				return;
			}
			if (OverlapTester.pointInRectangle(soundBounds, touchPoint.x, touchPoint.y)) {
				Assets.playSound(Assets.clickSound);
				//Settings.toggleSound();
 				if (Settings.soundEnabled)
					Assets.music.play();
				else
					Assets.music.pause();
			}*/
		}
	}

	public void draw (float deltaTime) {
		GLCommon gl = Gdx.gl;
		gl.glClearColor(1, 0, 0, 1);
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		guiCam.update();
		batcher.setProjectionMatrix(guiCam.combined);

		batcher.disableBlending();
		batcher.begin();
		batcher.draw(Assets.backgroundRegion, 0, 0, 320, 480);
		batcher.end();

		batcher.enableBlending();
		batcher.begin();
		batcher.draw(Assets.yourMove, 160 - 240 / 2, 480 - 40 - 52, 240, 52);
		batcher.draw(Assets.newGame, 160 - 240 / 2, 480 - 92 - 50, 240, 50);
		//batcher.draw(Assets.yourMove, 2, 200, 240, 50);
		//batcher.draw(Assets.yourMove, 2, 200, 240, 50);

		//batcher.draw(Assets.mainMenu, 10, 200 - 110 / 2, 300, 110);
		//batcher.draw(Settings.soundEnabled ? Assets.soundOn : Assets.soundOff, 0, 0, 64, 64);
		batcher.end();
	}

	@Override
	public void render (float delta) {
		update(delta);
		draw(delta);
	}
}
